<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>

<%@page import="java.util.*, model.*"%>
<%@ page import="java.sql.*, javax.sql.*, org.postgresql.*, java.util.concurrent.atomic.*"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Applications</title>
</head>
<body>

	<logic:notPresent parameter="reviewer">

	<%
	String database = "postgres";
	String username = "postgres";
	String password = "postgres";
	String first_name = "";
	String last_name = "";
	String middle_i = "";
	String discipline = (String)request.getParameter("discipline");
	String specialization = (String)request.getParameter("specialization");
	String name = "";
	int[] applicant_id;
	int j = 0;
	
	
	//DO THE ACTIONS!
	Connection conn = DriverManager.getConnection("jdbs:postgresql:" + database, username, password);
	if(request.getParameter("discipline") != null){
	
		// get country
		// creates statement to query
		Statement stmt = conn.createStatement(
                ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_UPDATABLE);
		ResultSet rs = stmt.executeQuery("SELECT applicant.id FROM public.specialization, public.degree, public.applicant WHERE degree.discipline =" + discipline +" AND degree.owner = applicant.pid GROUP BY applicant.id;");
		
		while(rs.next()){
		  j++;
		}
		rs.first();
		applicant_id = new int[j];
		for(int i = 0; i < j; i++){
			applicant_id[i] = rs.getInt(1);
			rs.next();
		}
		
		rs = stmt.executeQuery("SELECT discipline.name FROM public.discipline WHERE discipline.id =" + discipline +";");
		rs.next();
		name = rs.getString(1);
		out.println("<h2>Applicants with a "+name+" discipline</h2>");
		
		stmt.close();
	
	}else{
		if(request.getParameter("specialization") != null){
			Statement stmt = conn.createStatement(
	                ResultSet.TYPE_SCROLL_INSENSITIVE,
	                ResultSet.CONCUR_UPDATABLE);
			ResultSet rs = stmt.executeQuery("SELECT applicant.id FROM public.specialization, public.degree, public.applicant WHERE applicant.specialization =" + specialization +" AND degree.owner = applicant.pid GROUP BY applicant.id;");
		
			while(rs.next()){
			  j++;
			}
			rs.first();
			applicant_id = new int[j];
			for(int i = 0; i < j; i++){
				applicant_id[i] = rs.getInt(1);
				rs.next();
			}
			
			rs = stmt.executeQuery("SELECT specialization.name FROM public.specialization WHERE specialization.id =" + specialization +";");
			rs.next();
			name = rs.getString(1);
			out.println("<h1>Applicants with a specialization in "+name+"</h1>");
			
			stmt.close();
		}else{
			Statement stmt = conn.createStatement(
	                ResultSet.TYPE_SCROLL_INSENSITIVE,
	                ResultSet.CONCUR_UPDATABLE);
			ResultSet rs = stmt.executeQuery("SELECT applicant.id FROM public.applicant;");
			
			while(rs.next()){
				  j++;
				}
				rs.first();
				applicant_id = new int[j];
				for(int i = 0; i < j; i++){
					applicant_id[i] = rs.getInt(1);
					rs.next();
				}
				
				stmt.close();
		}
	}
	
	for(int i = 0; i < applicant_id.length; i++){
		PreparedStatement stmt1 = conn
		.prepareStatement("SELECT applicant.first_name, applicant.last_name, applicant.middle_ini FROM public.applicant WHERE applicant.id = ?;");
			stmt1.setInt(1, applicant_id[i]);
			// catches result
			try {
				ResultSet rs = stmt1.executeQuery();
				rs.next();
				first_name = rs.getString(1);
				last_name = rs.getString(2);
				middle_i = rs.getString(3);
			} catch (SQLException e) {
				out.println("fail");
			}
			out.println(first_name + " " + middle_i + " " + last_name + "</br>" );
			stmt1.close();
		}
	%>
	
	</logic:notPresent>
	
	<logic:present parameter="reviewer">
	Applications By Reviewer
		<%
		Vector<App> applicationVector = (Vector) session.getAttribute("applicationVector");
		int num = applicationVector.size();
		App app = new App();
		String grade;
			
		for(int i = 0; i < num; i++){ 
			app = applicationVector.get(i);
			if(app.getAverageGrade()==-1){
				grade = "Not Graded";
			}else
			{
				grade = ((Integer)app.getAverageGrade()).toString();
			}
	%>
			
			<li>
				<%out.println(app.getFirstName() + " " + app.getLastName()+ ", " + grade + "<br>Status: " + app.getAppStatus()); %>
				
				<%if(app.getAppStatus().equals("Admitted")){ %>
						<html:form action="/decision">
							<html:hidden property="userId" value="<%= ((Integer)app.getUserId()).toString() %>" />
							<html:hidden property="decision" value="Rejected" />
							<html:submit value="Reject" />
						</html:form>
						<html:form action="/decision">
							<html:hidden property="userId" value="<%= ((Integer)app.getUserId()).toString() %>" />
							<html:hidden property="decision" value="Pending" />
							<html:submit value="Cancel Decision" />
						</html:form>
					<% }else if(app.getAppStatus().equals("Rejected")) { %>
						<html:form action="/decision">
							<html:hidden property="userId" value="<%= ((Integer)app.getUserId()).toString() %>" />
							<html:hidden property="decision" value="Admitted" />
							<html:submit value="Admit" />
						</html:form>
						<html:form action="/decision">
							<html:hidden property="userId" value="<%= ((Integer)app.getUserId()).toString() %>" />
							<html:hidden property="decision" value="Pending" />
							<html:submit value="Cancel Decision" />
						</html:form>
					<% } else { %>
						<html:form action="/decision">
							<html:hidden property="userId" value="<%= ((Integer)app.getUserId()).toString() %>" />
							<html:hidden property="decision" value="Admitted" />
							<html:submit value="Admit" />
						</html:form>
						<html:form action="/decision">
							<html:hidden property="userId" value="<%= ((Integer)app.getUserId()).toString() %>" />
							<html:hidden property="decision" value="Rejected" />
							<html:submit value="Reject" />
						</html:form>
					<% } %>
					<br>
				
			</li>
		
		
		<%} %>
	</logic:present>

</body>
</html>